{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1c99cc32",
   "metadata": {},
   "source": [
    "## 数组形状操作\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7ecd2565",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "tang_array =np.arange(10)\n",
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dcda7b94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1edbf1eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang_array.shape =2,5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f3648bbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b0b8d55b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.reshape(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f7ff1bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "大小必须不能改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cf9e5287",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'numpy.ndarray' object attribute 'reshape' is read-only",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m tang_array\u001b[38;5;241m.\u001b[39mreshape \u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object attribute 'reshape' is read-only"
     ]
    }
   ],
   "source": [
    "tang_array.shape =3,4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "38e4699e",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang_array = np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4325bc99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 1, 1, 1, 1, 1, 10)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array = tang_array[:,np.newaxis,np.newaxis]\n",
    "tang_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "273532bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array = tang_array.squeeze()\n",
    "tang_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "acb3074b",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang_array.shape =2,5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "36073b49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8288a0ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5],\n",
       "       [1, 6],\n",
       "       [2, 7],\n",
       "       [3, 8],\n",
       "       [4, 9]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.transpose()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f6e88f7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5],\n",
       "       [1, 6],\n",
       "       [2, 7],\n",
       "       [3, 8],\n",
       "       [4, 9]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "87ea4ca7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af5cc6bc",
   "metadata": {},
   "source": [
    "## 数组的链接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "05ae0d6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 456, 780],\n",
       "       [234, 567, 898]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[123,456,780],[234,567,898]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a5a92454",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[321, 654, 721],\n",
       "       [432, 765, 925]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[321,654,721],[432,765,925]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c27992e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 456, 780],\n",
       "       [234, 567, 898],\n",
       "       [321, 654, 721],\n",
       "       [432, 765, 925]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.concatenate((a,b))\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f9670a83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 456, 780],\n",
       "       [234, 567, 898],\n",
       "       [321, 654, 721],\n",
       "       [432, 765, 925]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.concatenate((a,b),axis =0)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "93357095",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 456, 780, 321, 654, 721],\n",
       "       [234, 567, 898, 432, 765, 925]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.concatenate((a,b),axis =1)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8a4c45f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 6)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e9071486",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 456, 780],\n",
       "       [234, 567, 898],\n",
       "       [321, 654, 721],\n",
       "       [432, 765, 925]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0b06347a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 456, 780, 321, 654, 721],\n",
       "       [234, 567, 898, 432, 765, 925]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "62ad5430",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 456, 780],\n",
       "       [234, 567, 898]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4a4667e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([123, 456, 780, 234, 567, 898])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "600ae2ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 456, 780],\n",
       "       [234, 567, 898]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "d590449f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([123, 456, 780, 234, 567, 898])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbb1808b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
